package com.store.app.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.R;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.Utils;

/**
 * 废弃
 * 直接赠送礼券
 */
public class GoSendFragment extends Fragment implements IOnHttpListener {
    private View v;
    private NetRequest netRequest;
    private EditText et_phone;
    private EditText et_coupon;
    private TextView submit;
    private volatile ProgressDialog mProgressDialog;
    private int tempCoupon = 0;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_go_send, container, false);
        initView(v);
        return v;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }


    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        Toast.makeText(getActivity(),"网络不通,赠送失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if(requestId==1){
            Toast.makeText(getActivity(),"赠送礼券成功",Toast.LENGTH_SHORT).show();
            et_phone.setText("");
            try {
                Stores.voucher_balance = Stores.voucher_balance - tempCoupon;
            }catch (Exception e){
                e.printStackTrace();
                Log.v("zyl", "礼券运算错误");
            }
            et_coupon.setText("");
            et_coupon.setText("");
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        if(requestId==1){
            Toast.makeText(getActivity(),"赠送礼券失败:"+msg,Toast.LENGTH_SHORT).show();
        }
    }
    protected void dismissProgressDialog() {
        try {
            if (mProgressDialog != null)
                mProgressDialog.dismiss();
        }catch (Exception e){

        }
    }
    protected void showProgressDialog(String title, String msg) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getActivity());
        }
        if(!TextUtils.isEmpty(title)){
            mProgressDialog.setTitle(title);}
        mProgressDialog.setMessage(msg);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }
    private void initView(View v) {
        netRequest = new NetRequest(this,getActivity());
        netRequest.requestVerifyUserToken(2);
        et_phone = (EditText) v.findViewById(R.id.et_phone);
        et_coupon = (EditText) v.findViewById(R.id.et_coupon);
        submit = (TextView) v.findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit() {
        String phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getActivity(), "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(!Utils.matchPhone(phone)){
            Toast.makeText(getActivity(), "手机号格式不对!", Toast.LENGTH_SHORT).show();
            return;
        }
        String coupon = et_coupon.getText().toString().trim();
        if (TextUtils.isEmpty(coupon)|coupon.startsWith("0")) {
            Toast.makeText(getActivity(), "赠送礼券不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(Stores.voucher_balance>=0){
            try{
                tempCoupon=Integer.parseInt(coupon);
                if(Stores.voucher_balance<tempCoupon){
                    Toast.makeText(getActivity(), "您的礼券不足！", Toast.LENGTH_SHORT).show();
                    return;
                }
            }catch (Exception e){
                Toast.makeText(getActivity(), "您的礼券不足！", Toast.LENGTH_SHORT).show();
                return;
            }
            if(tempCoupon>200){
                Toast.makeText(getActivity(), "礼券单次赠送不能超过200！", Toast.LENGTH_SHORT).show();
                return;
            }
            if(tempCoupon<10){
                Toast.makeText(getActivity(), "礼券单次赠送不能少于10！", Toast.LENGTH_SHORT).show();
                return;
            }

        }else{
            Toast.makeText(getActivity(), "您的礼券不足！", Toast.LENGTH_SHORT).show();
            return;
        }
        showProgressDialog("","正在赠送礼券...");

        netRequest.requestSendCoupon(1,phone,coupon);
    }
}